function [options, bAbort] = TLUpgradeDlg(modelName, revisionNumber, bAutostart)
% TL_UPGRADE_DLG provides a GUI for input of upgrade options
%
% SYNTAX options = TLUpgradeDlg(modelName, revisionNumber, bAutostart)
%   INPUT ARGUMENTS
%   modelName           name of the model
%   revisionNumber      current model's revision number
%   bAutostart          signals that upgrade was invoked from tl_postloadfcn
%
%   OUTPUT ARGUMENTS
%   options             options for model upgrade
%   bAbort               signals user abort of upgrade process


% Author(s): R. Wiebeziek
% $RCSfile: TLUpgradeDlg.m $
% $ProjectName: e:/ARC/Components/HostSW/ProdCodeGen/BlockDataValidator/Sources/MSrc/project.pj $
% $Revision: 1.8 $
% $Date: 2009/03/16 16:26:09MEZ $
%
% Copyright ?2009 dSPACE GmbH. All rights reserved.


if strcmp(ds_error_get('Batchmode'),'on'),
     options = [];
     bAbort = 0;
   return
end

bRevisionBelow100         = revisionNumber < 100;
options.bUseTL2xPortNames = ~bRevisionBelow100;
options.bRebuildSFcn      = bRevisionBelow100;
options.bUpgradeLibs      = true;

% if upgrade was invoked from tl_postloadfcn add info text to dialog
if bAutostart
   msg = {'The model',['   ' modelName], 'needs upgrading.', ...
          '', ...
          'Without upgrading, numerous errors will most likely occur when working with TargetLink.', ...
          '', ...
          'Do you wish to upgrade the model now?'};
else
    if bRevisionBelow100
        msg = {'Model upgrade routine of TargetLink.', ...
          'You may choose to use the port names from former TargetLink version' ...
          'and to include referenced libraries in the upgrade process.'};
    else
        msg = {'Model upgrade routine of TargetLink.', ...
          'You may choose to rebuild S-Functions and to include referenced libraries in the upgrade process.'};
    end
end

%---------------------------------------
% dialog documentation
%---------------------------------------

frame_1 = { ...
   'Frame',             'frame_1', ...
   'Tag',               'frame_1', ...
   'Style',             'frame', ...
   'FrameWidth',        400, ...
   'DisplayText',       'Model Upgrade' ...
   };

blockDocu = { ...
   'Tag',               'blockDocumentation', ...
   'Style',             'text', ...
   'MessageText',       msg ...
   };

%---------------------------------------
% checkboxes
%---------------------------------------

frame_2 = { ...
   'Frame',             'frame_2', ...
   'Tag',               'frame_2', ...
   'Style',             'frame', ...
   'FrameWidth',        400, ...
   'DisplayText',       'Upgrade Options' ...
   };

if bRevisionBelow100
    bPortnamesOrSFcn = { ...
        'Tag',               'bPortnamesOrSFcn', ...
        'Style',             'checkbox', ...
        'InitialValue',      1, ...
        'LabelStrings',      'Use port names from older TargetLink version' ...
        };
else
    bPortnamesOrSFcn = { ...
        'Tag',               'bPortnamesOrSFcn', ...
        'Style',             'checkbox', ...
        'InitialValue',      1, ...
        'LabelStrings',      'Rebuild S-Functions' ...
        };
end

bUpgradeLibs = { ...
   'Tag',               'bUpgradeLibs', ...
   'Style',             'checkbox', ...
   'InitialValue',      1, ...
   'LabelStrings',      'Unlock and upgrade libraries' ...
   };

while 1,
   userData = ds_generic_dlg('Create', ...
      'FigureTag',         '#Fig_TLUpgradeDlg', ...
      'Title',             'TargetLink Upgrade Utility', ...
      'WindowStyle',       'modal', ...
      frame_1{:}, ...
      blockDocu{:}, ...
      frame_2{:}, ...
      bPortnamesOrSFcn{:}, ...
      bUpgradeLibs{:}, ...
      'buttonName',        'Help', ...
      'buttonName',        'Skip', ...
      'buttonName',        'Upgrade', ...
      'DefaultButton',     'Upgrade' ...
      );
   if strcmpi(userData.currentButton,'Help')
      tl_online_help('topic','tl_upgrade');
   elseif any(strcmpi(userData.currentButton,{'Skip','Upgrade'})),
      break;
   end
end

if bRevisionBelow100
    options.bUseTL2xPortNames = userData.uiControl(userData.dataDescription.bPortnamesOrSFcn.uiControl).value;
else
    options.bRebuildSFcn = userData.uiControl(userData.dataDescription.bPortnamesOrSFcn.uiControl).value;
end
options.bUpgradeLibs = userData.uiControl(userData.dataDescription.bUpgradeLibs.uiControl).value;
bAbort = ~strcmpi(userData.currentButton,'Upgrade');
